package com.example.service.impl;

import com.example.constant.Constant;
import com.example.exception.MyException;
import com.example.mapper.DormitoryMapper;
import com.example.pojo.Dormitory;
import com.example.pojo.PageBean;
import com.example.service.DormitoryService;
import com.example.util.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class DormitoryServiceImpl implements DormitoryService {

    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public void addDormitory(Dormitory dormitory) {
        dormitory.setCreateTime(LocalDateTime.now());
        dormitory.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        dormitory.setCreateUser(id);

        dormitoryMapper.addDormitory(dormitory);
    }

    @Override
    public void updateDormitory(Dormitory dormitory) {
        dormitory.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        dormitory.setCreateUser(id);
        dormitoryMapper.updateDormitory(dormitory);
    }

    @Override
    public Dormitory getDormitoryById(Integer id) {
        Dormitory dormitory = dormitoryMapper.getDormitoryById(id);
        return dormitory;
    }

    @Override
    public void deleteDormitory(Integer id) {
        dormitoryMapper.deleteDormitory(id);
    }

    @Override
    public PageBean listDormitory(Integer pageNum, Integer pageSize,Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Page<Dormitory> page = dormitoryMapper.listDormitory(id,categoryId);
        PageBean pageBean = new PageBean();
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());
        return pageBean;
    }

    @Override
    public List<Dormitory> list() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        List<Dormitory> list1 = dormitoryMapper.list(id);
        return list1;
    }

    @Override
    public List<Dormitory> findAll() {
        List<Dormitory> list =dormitoryMapper.findAll();
        return list;
    }

    @Override
    public List<Dormitory> findByZhiYe() {
        List<Dormitory> list = dormitoryMapper.findByZhiYe();
        return list;
    }

    @Override
    public List<Dormitory> findByShuangChuangSiRen() {
        List<Dormitory> list = dormitoryMapper.findShuangChuangSiRen();
        return list;
    }

    @Override
    public List<Dormitory> findByShuangChuangShuangRen() {
        List<Dormitory> list = dormitoryMapper.findShuangChuangShuangRen();
        return list;
    }

    @Override
    public List<Dormitory> findByQiYiSiRen() {
        List<Dormitory> list = dormitoryMapper.findQiYiSiRen();
        return list;
    }

    @Override
    public List<Dormitory> findByYaoLinYaoSiRen() {
        List<Dormitory> list = dormitoryMapper.findByYaoLinYaoSiRen();
        return list;
    }

    @Override
    public List<Dormitory> findByShiJiuSiRen() {
        List<Dormitory> list = dormitoryMapper.findByShiJiuSiRen();
        return list;
    }

    @Override
    public List<Dormitory> findExcludeFullByZhiYe() {
        List<Dormitory> list = dormitoryMapper.findExcludeFullByZhiYe();
        return list;
    }
}
